<?php


namespace app\apiadmin\controller;


use app\apiadmin\service\system\MemberService;
use app\common\controller\BaseController;
use app\common\service\CaptchaService;
use app\common\utils\Res;
use app\common\utils\SmsHelper;
use app\service\sys\ToolService;
use app\service\sys\UserBehaviorService;
use think\facade\Cache;

class Login extends BaseController
{
    
    /**
     * 发送短信验证码
     * 
     * @return mixed
     */
    public function sendVerifyCode()
    {
        $param = $this->request->param();
        $phone = $param['phone'];
        $code = ToolService::generateSMSCode();
        $sendCode = SmsHelper::sendCaptchaSms([$phone], [$code]);
        if ($sendCode['Code'] == 'OK') {
            UserBehaviorService::generatePhoneCode($phone, $code);
        }
        return Res::ok($sendCode);
    }

    /**
     * 短信验证码登录
     *
     * @return mixed
     */
    public function phoneLogin() {
        $param = $this->request->param();
        return MemberService::phoneLogin($param);
    }

    /**
     * 图片验证码
     * @return mixed
     */
    public function verify() {
        return app()->make(CaptchaService::class)->create();
    }

    /**
     * 账号密码登录
     * @return mixed
     */
    public function login() {
        $param = $this->request->post();
        return MemberService::login($param);
    }

    /**
     * 登出
     * @return mixed
     */
    public function logout() {
        $res = UserBehaviorService::logLogoutToken();
        return Res::ok($res);
    }

    /**
     * 清空缓存
     * @return mixed
     */
    public function clear() {
        //Cache::delete('');
        return Res::ok();
    }

}